//
//  StepperViewController.swift
//  DemoProject
//
//  Created by hao on 2018/7/18.
//  Copyright © 2018年 hao. All rights reserved.
//

import UIKit

class StepperViewController: BaseTableViewController {

    let disposeBag = DisposeBag()
    
    lazy var stepper: UIStepper = {
        let _stepper = UIStepper()
        self.tableHeadView.addSubview(_stepper)
        _stepper.snp.makeConstraints({ (make) in
            make.center.equalTo(self.tableHeadView)
            make.width.equalTo(100)
            make.height.equalTo(44)
        })
        return _stepper
    }()
    
    lazy var label: UILabel = {
        let _label = UILabel()
        self.tableHeadView.addSubview(_label)
        _label.snp.makeConstraints({ (make) in
            make.centerX.equalTo(self.tableHeadView)
            make.bottom.equalTo(stepper.snp.top).offset(-20)
        })
        return _label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        stepper.minimumValue = 1
        stepper.maximumValue = 5
        stepper.value = 2.5
        // 每次增减的值
        stepper.stepValue = 0.5
        // 设置stepper可以按住不放来连续更改值
        stepper.isContinuous = true
        // 设置stepper是否循环（到最大值时再增加数值从最小值开始）
        stepper.wraps = true
        // 替换 ‘-’ 图标
        stepper.setDecrementImage(UIImage(named: "btn_last_play")?.withRenderingMode(.alwaysOriginal), for: .normal)
        // 替换 ‘+’ 图标
        stepper.setIncrementImage(UIImage(named: "btn_next_play")?.withRenderingMode(.alwaysOriginal), for: .normal)
        // 设置背景图片
        stepper.setBackgroundImage(UIImage(named: "view_backgeound"), for: .normal)
        // 设置分割线
        stepper.setDividerImage(UIImage(named: "buddy_header_bg"), forLeftSegmentState: .normal, rightSegmentState: .normal)
        setupData()
        _rxSwiftProperty()
    }
    
    private func _rxSwiftProperty() {
        stepper.rx.value.asObservable().subscribe(onNext: { print(info: "value: \($0)") }).disposed(by: disposeBag)
        
        stepper.rx.value.map { "当前值为: \($0)" }.bind(to: label.rx.text).disposed(by: disposeBag)
    }
    
    private func setupData() {
        let titles = ["minimumValue",
                      "maximumValue",
                      "value",
                      "stepValue",
                      "continuous",
                      "wraps",
                      "替换 ‘-’ 图标",
                      "替换 ‘+’ 图标",
                      "设置背景图片",
                      "设置分割线"]
        
        let descArr = ["设置stepper范围的最小值",
                       "设置stepper范围的最大值",
                       "当前值",
                       "设置每次增减的值",
                       "设置stepper可以按住不放来连续更改值",
                       "设置stepper是否循环（到最大值时再增加数值从最小值开始）",
                       "stepper.setDecrementImage(UIImage(named: \"btn_last_play\")?.withRenderingMode(.alwaysOriginal), for: .normal)",
                       "stepper.setIncrementImage(UIImage(named: \"btn_next_play\")?.withRenderingMode(.alwaysOriginal), for: .normal)",
                       "stepper.setBackgroundImage(UIImage(named: \"view_backgeound\"), for: .normal)",
                       "stepper.setDividerImage(UIImage(named: \"buddy_header_bg\"), forLeftSegmentState: .normal, rightSegmentState: .normal)"]
        
        addTitles(titles, footDescs: descArr)
    }

}
